package org.cnam.mobileShopping.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.cnam.mobileShopping.bean.User;
import org.cnam.mobileShopping.sql.DbAdapter;
import org.cnam.mobileShopping.wsclient.WSClient;
import org.json.JSONException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDAO extends DAO<User> {
	private static final String tableName = "user";

	public UserDAO(Context context) {
		sqlHelper = new DbAdapter(context);
	}

	@Override
	public long create(User obj) {
		// récupération d'un user dans la base de données locale
		SQLiteDatabase db = sqlHelper.getWritableDatabase();

		Calendar cal = Calendar.getInstance();
		ContentValues cv = new ContentValues();
		cv.put("login", obj.getLogin());
		cv.put("pseudo", obj.getPseudo());
		cv.put("commonid", obj.getCommonid());
		long toReturn = db.insert(tableName, "", cv);

		db.close();
		return toReturn;
	}

	@Override
	public boolean delete(User obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(User obj) {
		return true;
	}

	@Override
	public User find(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAll() {
		List<User> toReturn = new ArrayList<User>();
		// envoit du produit à la base de données distante
		// TODO Send to the web service
		// récupération d'un produit dans la base de données locale
		SQLiteDatabase db = sqlHelper.getReadableDatabase();

		Cursor cursor = db.query("user", new String[] { "userid", "login",
				"pseudo", "commonid" }, "", null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				User u = new User();
				u.setUserid(cursor.getInt(0));
				u.setLogin(cursor.getString(1));
				u.setPseudo(cursor.getString(2));
				u.setCommonid(cursor.getString(3));
				toReturn.add(u);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		db.close();
		return toReturn;
	}

	@Override
	public void synchronise(Date lastupdate, Integer userId, Context context) {
		List<User> users = null;
		try {
			users = WSClient.getInstance().getUsers();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (users != null) {
			for (User u : users) {
				if (findByCommonId(u.getCommonid()) == null) {
					// si le user n'existe pas en base, on le cree
					create(u);
				}
			}
		}
	}

	@Override
	public User findByCommonId(String commonid) {
		User toReturn = null;
		SQLiteDatabase db = sqlHelper.getReadableDatabase();

		Cursor cursor = db.query("user", new String[] { "userid", "login",
				"pseudo", "commonid" }, "", null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				toReturn = new User();
				toReturn.setUserid(cursor.getInt(0));
				toReturn.setLogin(cursor.getString(1));
				toReturn.setPseudo(cursor.getString(2));
				toReturn.setCommonid(cursor.getString(3));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		db.close();
		return toReturn;
	}

}
